using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author:Weiyudong
    /// </summary>
    /// 
    public partial class SCDutyShiftEntry : BasePage_SOM
    {
       
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140815";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
            }
        }
        /// <summary>
        /// init the page;
        /// </summary>
        private void SetPageLoad()
        {
            if (this.Request["shiftID"] != null)//the record is existed
            {
                DataTable dt = new DataTable();
                dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDShift.GetShiftDetailByID(int.Parse(this.Request["shiftID"].ToString()));
                if (dt.Rows.Count == 0)
                {
                    return;
                }
                this.hidID.Value = dt.Rows[0]["shiftID"].ToString();
                this.txtShiftCode1.Text = dt.Rows[0]["Code1"].ToString();
                this.txtShiftCode2.Text = dt.Rows[0]["Code2"].ToString();
                this.txtShiftCode3.Text = dt.Rows[0]["Code3"].ToString();
                this.txtDescription.Text = dt.Rows[0]["Description"].ToString();
                this.txtOrder.Text = dt.Rows[0]["Order"].ToString();
                this.txtShiftTimeFrom.Text = dt.Rows[0]["ShiftTimeFrom"].ToString();
                this.txtShiftTimeTo.Text = dt.Rows[0]["ShiftTimeTo"].ToString();
                this.chkVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());

                if (this.chkVoid.Checked)
                {
                    AfterVoid();
                }
                dt.Dispose();
                dt = null;

            }
            else//new record page
            {
                this.chkVoid.Enabled = false;
            }

            //bind confirm function on btnVoid
            //string bindScript = "if (confirm('" + this.GetMessage("SOM_VoidSure") + "') )";

            //bindScript = bindScript + "{return true;}";
            //bindScript = bindScript + "else";
            //bindScript = bindScript + "{return false;}";
            //bindScript = bindScript + "";

            //this.btnVoid.Attributes.Add("onclick", bindScript);

        }

        #endregion

        #region SetLanguage

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }
        /// <summary>
        /// for multi language
        /// </summary>
        private void SetLanguage()
        {

            this.lblTitle.Text = this.GetLabel("SOM_SCDutyShiftEntryTitle");
            this.lblShiftCode1.Text = this.GetLabel("SOM_ShiftCode1", true);
            this.lblShiftCode2.Text = this.GetLabel("SOM_ShiftCode2", true);
            this.lblShiftCode3.Text = this.GetLabel("SOM_ShiftCode3", true);
            this.lblDescription.Text = this.GetLabel("COM_Description", true);
            this.lblOrder.Text = this.GetLabel("SOM_ShiftOrder", true);
            this.lblShiftTimeFrom.Text = this.GetLabel("SOM_ShiftTimeFrom", true);
            this.lblShiftTimeTo.Text = this.GetLabel("SOM_ShiftTimeTo", true);
            this.chkVoid.Text = this.GetLabel("SOM_Void", true);
            this.btnSave.Text = GetLabel("COM_Save");
            this.btnSaveAndNew.Text = GetLabel("SOM_SaveNew");
            this.btnSaveAndReturn.Text = GetLabel("COM_SaveAndReturn");
            this.btnCancel.Text = GetLabel("COM_Cancel");
            //this.btnSave.Text = this.GetLabel("COM_Save");
            //this.btnVoid.Text = this.GetLabel("SOM_VoidOperation");           
            //this.btnNew.Text = this.GetLabel("SOM_NewSCDutyShift");
            if (this.LangCode == "1")
            {
                this.txtDescription.Width = 280;
            }
        }
        #endregion

        #region self custom functinon
        /// <summary>
        /// when void the record,set the record readonly
        /// </summary>
        private void AfterVoid()
        {
            this.txtShiftCode1.Enabled = false;
            this.txtShiftCode2.Enabled = false;
            this.txtShiftCode3.Enabled = false;
            this.txtDescription.Enabled = false;
            this.txtOrder.Enabled = false;
            this.txtShiftTimeFrom.Enabled = false;
            this.txtShiftTimeTo.Enabled = false;

            this.chkVoid.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
            //this.chkVoid.Checked = true;
            //this.btnSave.Enabled = false;
            //this.btnVoid.Enabled = false;

        }
        /// <summary>
        /// new\update\void the record
        /// </summary>
        /// <param name="isolateReasonID">in and out parm,be out parm just when the new a record</param>
        /// <param name="action">1 new ; 2 update ; 3 void </param>
        /// <returns>1 success; other fail</returns>
        private int Save(ref int shiftID, int action)
        {
            return MTRC.ODMS.DAL.SOM.CodeSetup.CDShift.UpdateShift(
                                                   ref shiftID,
                                                   this.txtShiftCode1.Text.Trim(),
                                                   this.txtShiftCode2.Text.Trim(),
                                                   txtShiftCode3.Text.Trim(),
                                                   this.chkVoid.Checked,
                                                   this.ODMUser.ID,
                                                   this.ODMUser.ID,
                                                   action,
                                                   this.txtDescription.Text.Trim(),
                                                   Convert.ToInt16(txtOrder.Text.Trim()),
                                                   this.txtShiftTimeFrom.Text.Trim(),
                                                   this.txtShiftTimeTo.Text.Trim()
                                                   );
        }

        private void DealSave(int type)
        {
            if (!this.CheckMandatory())
            {
                return;
            }
            //else if (this.txt)
            int id = 0;
            if (this.chkVoid.Checked == false)//update or new , not void
            {
                if (this.hidID.Value == String.Empty)//new
                {

                    int mResult = Save(ref id, 1);
                    if (mResult == 11)
                    {
                        this.Alert(this.GetMessage("SOM_Code1Unique"));

                    }
                    else if (mResult == 12)
                    {
                        this.Alert(this.GetMessage("SOM_Code2Unique"));

                    }
                    else if (mResult == 13)
                    {
                        this.Alert(this.GetMessage("SOM_Code3Unique"));

                    }
                    else if (mResult == 14)
                    {
                        this.Alert(this.GetMessage("SOM_OrerUniqueError"));

                    }
                    else if (mResult == 1)
                    {
                        this.hidID.Value = id.ToString();

                        this.Alert(this.GetMessage("SaveSuccessfully"));
                        if (type == 1)//save
                        {
                            this.chkVoid.Enabled = true;
                            //this.Response.Redirect("SCDutyShiftEntry.aspx?shiftID=" + this.hidID.Value);
                            //string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftEntry.aspx?shiftID='" + this.hidID.Value + "'} window.setTimeout(btnChange_Click,50);" + "</script>";
                            //this.Page.RegisterStartupScript("aaa", script);
                        }
                        else if (type == 2)//save and return
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("SCDutyShiftList.aspx");
                        }
                        else if (type == 3)
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftEntry.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("SCDutyShiftEntry.aspx");
                        }
                    }

                    //Page.Response.Redirect("IsolateReasonEntry.aspx?isolationReasonID=" + id);

                }
                else//update
                {

                    id = int.Parse(this.hidID.Value);
                    int mResult = Save(ref id, 2);
                    if (mResult == 2)
                    {
                        this.Alert(this.GetMessage("SOM_SCDutyShiftVoidError"));

                    }
                    else if (mResult == 11)
                    {
                        this.Alert(this.GetMessage("SOM_Code1Unique"));

                    }
                    else if (mResult == 12)
                    {
                        this.Alert(this.GetMessage("SOM_Code2Unique"));

                    }
                    else if (mResult == 13)
                    {
                        this.Alert(this.GetMessage("SOM_Code3Unique"));

                    }
                    else if (mResult == 14)
                    {
                        this.Alert(this.GetMessage("SOM_OrerUniqueError"));

                    }
                    else if (mResult == 1)
                    {
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        if (type == 1)//save
                        {
                            //this.chkVoid.Enabled = true;
                        }
                        else if (type == 2)//save and return
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("SCDutyShiftList.aspx");
                        }
                        else if (type == 3)
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftEntry.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("SCDutyShiftEntry.aspx");
                        }
                    }
                    else
                    {
                        this.Alert(this.GetMessage("SaveUnsuccessfully"));
                    }


                }
            }
            else
            {
                if (this.hidID.Value != String.Empty)
                {
                    int mId = int.Parse(this.hidID.Value);
                    this.chkVoid.Checked = true;
                    int mResult = Save(ref mId, 3);

                    if (mResult == 1)
                    {
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        if (type == 1)//save
                        {
                            AfterVoid();
                        }
                        else if (type == 2)//save and return
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("SCDutyShiftList.aspx");
                        }
                        else if (type == 3)
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='SCDutyShiftEntry.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("SCDutyShiftEntry.aspx");
                        }
                        
                    }
                    else
                    {
                        this.Alert(this.GetMessage("SOM_SCDutyShiftCodeVoidError"));
                    }


                }
            }
        }
        private bool CheckMandatory()
        {
            if (this.txtShiftCode1.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_Code1NotNull"));
                return false;
            }
            else if (this.txtShiftCode2.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_Code2NotNull"));
                return false;
            }
            else if (this.txtShiftCode3.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_Code3NotNull"));
                return false;
            }
            else if (this.txtDescription.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_DescriptionNotNull"));
                return false;
            }
            else if (this.txtOrder.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_OrderNotNull"));
                return false;
            }
            else if (this.txtShiftTimeFrom.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_FromTimeNotNull"));
                return false;
            }
            else if (this.txtShiftTimeTo.Text.Trim() == String.Empty)
            {
                this.Alert(this.GetMessage("SOM_ToTimeNotNull"));
                return false;
            }
           
            string timeFrom = this.txtShiftTimeFrom.Text.Trim();
            string timeTo = this.txtShiftTimeTo.Text.Trim();
            double HH = 0d;
            double MM = 0d;
            if (timeFrom.Length > 0)
            {
                if (!Regex.IsMatch(timeFrom, APJ.Util.RegexPattern.DateTime_Time))
                {
                    this.Alert(this.GetMessage("SOM_TimeStyleError"), 50, this.txtShiftTimeFrom);
                    return false;
                }
                if (!double.TryParse(timeFrom.Substring(0, 2), out HH) || !double.TryParse(timeFrom.Substring(3, 2), out MM))
                {
                    this.Alert(this.GetMessage("SOM_TimeStyleError"), 50, this.txtShiftTimeFrom);
                    return false;
                }
                if (HH > 23 || MM > 59)
                {
                    this.Alert(this.GetMessage("SOM_TimeStyleError"), 50, this.txtShiftTimeFrom);
                    return false;
                }
                
            }
            if (timeTo.Length > 0)
            {
                if (!Regex.IsMatch(timeTo, APJ.Util.RegexPattern.DateTime_Time))
                {
                    this.Alert(this.GetMessage("SOM_TimeStyleError"), 50, this.txtShiftTimeTo);
                    return false;
                }
                HH = 0d;
                MM = 0d;
                if (!double.TryParse(timeTo.Substring(0, 2), out HH) || !double.TryParse(timeTo.Substring(3, 2), out MM))
                {
                    this.Alert(this.GetMessage("SOM_TimeStyleError"), 50, this.txtShiftTimeTo);
                    return false;
                }
                if (HH > 23 || MM > 59)
                {
                    this.Alert(this.GetMessage("SOM_TimeStyleError"), 50, this.txtShiftTimeTo);
                    return false;
                }
                
            }
            return true;
        }

       
        #endregion

        #region Event
        /// <summary>
        /// save click, new and update 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.DealSave(1);
        }
       
        ///// <summary>
        ///// Void a isolateReason Record
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void btnVoid_Click(object sender, EventArgs e)
        //{
        //    
        //}
        #endregion

        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            this.DealSave(2);
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            this.DealSave(3);
        }

        protected void chkVoid_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkVoid.Checked)
            {
                string bindScript = "if (confirm('" + this.GetMessage("SOM_VoidSure") + "') )";

                bindScript = bindScript + "{return true;}";
                bindScript = bindScript + "else";
                bindScript = bindScript + "{return false;}";
                bindScript = bindScript + "";

                this.btnSave.Attributes.Add("onclick", bindScript);
                this.btnSaveAndNew.Attributes.Add("onclick", bindScript);
                this.btnSaveAndReturn.Attributes.Add("onclick", bindScript);
            }
            else
            {
                this.btnSave.Attributes.Clear();
                this.btnSaveAndReturn.Attributes.Clear();
                this.btnSaveAndNew.Attributes.Clear();
            }
        }
    }
}
